//
//  ArbitrationController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/17.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "ArbitrationController.h"
#import "ArbitrationModel.h"
#import "ArbitrationCell.h"
#import "ArbitrationDetailController.h"

@interface ArbitrationController ()
@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation ArbitrationController

static NSString *const ID = @"ArbitrationCell";

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _string;
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];

}

- (void)loadData{
    
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': sszcList,'service': %@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:-1}'}",_service,userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dic = dict[@"attrs"];
            ArbitrationModel *model = [ArbitrationModel mj_objectWithKeyValues:dic];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}
- (void)MoreData{
    
    self.currentPage ++;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': sszcList,'service': %@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:-1}'}",_service,userId,pwdId,userHumber,self.currentPage];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dic = dict[@"attrs"];
            ArbitrationModel *model = [ArbitrationModel mj_objectWithKeyValues:dic];
            
            [self.dataArray addObject:model];
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
     ArbitrationModel*model = self.dataArray[indexPath.row];
    
    ArbitrationCell *cell = [ArbitrationCell WithArbitrationCell];
    
    if (cell == nil) {
        
        cell = [[ArbitrationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    cell.model = model;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ArbitrationModel *model = self.dataArray[indexPath.row];
    
    ArbitrationDetailController *inD = [[ArbitrationDetailController alloc] init];
    
    inD.ID = model.ID;
    inD.service = _service;
    [self.navigationController pushViewController:inD animated:YES];
    
}

@end











